package com.qiyuan.web.controller.essay;

import java.util.List;
import java.util.Arrays;

import com.qiyuan.common.core.page.TableDataInfo;
import com.qiyuan.essay.domain.bo.BlogArticleBo;
import com.qiyuan.essay.domain.vo.BlogArticleVo;
import com.qiyuan.essay.service.IBlogArticleService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.qiyuan.common.annotation.RepeatSubmit;
import com.qiyuan.common.annotation.Log;
import com.qiyuan.common.core.controller.BaseController;
import com.qiyuan.common.core.domain.PageQuery;
import com.qiyuan.common.core.domain.AjaxResult;
import com.qiyuan.common.core.validate.AddGroup;
import com.qiyuan.common.core.validate.EditGroup;
import com.qiyuan.common.core.validate.QueryGroup;
import com.qiyuan.common.enums.BusinessType;
import com.qiyuan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 文章信息Controller
 *
 * @author ruoyi
 * @date 2022-04-16
 */
@Validated
@Api(value = "文章信息控制器", tags = {"文章信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/essay/article")
public class BlogArticleController extends BaseController {

    private final IBlogArticleService iBlogArticleService;

    /**
     * 查询文章信息列表
     */
    @ApiOperation("查询文章信息列表")
    @SaCheckPermission("essay:article:list")
    @GetMapping("/list")
    public TableDataInfo<BlogArticleVo> list(@Validated(QueryGroup.class) BlogArticleBo bo, PageQuery pageQuery) {
        return iBlogArticleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文章信息列表
     */
    @ApiOperation("导出文章信息列表")
    @SaCheckPermission("essay:article:export")
    @Log(title = "文章信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated BlogArticleBo bo, HttpServletResponse response) {
        List<BlogArticleVo> list = iBlogArticleService.queryList(bo);
        ExcelUtil.exportExcel(list, "文章信息", BlogArticleVo.class, response);
    }

    /**
     * 获取文章信息详细信息
     */
    @ApiOperation("获取文章信息详细信息")
    @SaCheckPermission("essay:article:query")
    @GetMapping("/{id}")
    public AjaxResult<BlogArticleVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iBlogArticleService.queryById(id));
    }

    /**
     * 新增文章信息
     */
    @ApiOperation("新增文章信息")
    @SaCheckPermission("essay:article:add")
    @Log(title = "文章信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BlogArticleBo bo) {
        return toAjax(iBlogArticleService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改文章信息
     */
    @ApiOperation("修改文章信息")
    @SaCheckPermission("essay:article:edit")
    @Log(title = "文章信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BlogArticleBo bo) {
        return toAjax(iBlogArticleService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除文章信息
     */
    @ApiOperation("删除文章信息")
    @SaCheckPermission("essay:article:remove")
    @Log(title = "文章信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iBlogArticleService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 修改文章属性信息
     */
    @ApiOperation("修改文章属性信息")
    @SaCheckPermission("essay:article:update")
    @Log(title = "文章信息", businessType = BusinessType.UPDATE)
    @GetMapping("updateSx/{ids}/{bs}")
    public AjaxResult<Void> updateSx(@PathVariable Long[] ids,@PathVariable("bs") String bs) {
        return toAjax(iBlogArticleService.updateSx(Arrays.asList(ids), bs,true) ? 1 : 0);
    }
}
